package m09;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
//import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener;
import java.io.Serializable;
import java.util.Random;

import javax.swing.JButton;

import m07.Good;
import m07.Player;
import m07.GamePanel;


/**
 * 
 * @author Jessica Roberts and Samantha Rich
 * @version 1.0 22 Oct 2012
 *
 */
public class TradePanel extends JPanel implements Serializable {
	private static final long serialVersionUID = -6486824438404941954L;
	private JButton waterToSell1,gamesToSell1,oreToSell1,foodToSell1,furToSell1,fireToSell1,robotsToSell1,machineToSell1,medsToSell1,narcToSell1,
		narcToBuy1,medsToBuy1,machinesToBuy1,robotToBuy1,fireToBuy1,furToBuy1,foodToBuy1,oreToBuy1,gamesToBuy1,waterToBuy1;
	private JLabel lblName1, lblMoney1;
	private JLabel waterP1, fursP1, foodP1, oreP1, gamesP1, firearmsP1, medicineP1, machinesP1, narcoticsP1, robotsP1;
	private Good waterGood1, furGood1, foodGood1, oreGood1, gamesGood1, fireGood1, medsGood1, machineGood1, narcGood1, robotGood1;
	private int level; 
	public GamePanel gp;
	public JFrame frame;
	private Icon shipIcon;
	
	/**
	 * Create the panel.
	 * @param boolean isForTrader true if it's for a trader, false otherwise
	 */
	public TradePanel(Trader t, GamePanel gp, JFrame frame) {
		activateGoods();
		setLayout(null);
		
		this.gp = gp;
		this.frame = frame;
		
		BufferedImage shipImg;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("ship.png");
			shipImg = ImageIO.read(input);
			shipIcon = new ImageIcon(shipImg);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Icon Not Loaded");
		}
		
		Random randomLevel = new Random();
		level = randomLevel.nextInt(8);
		
		lblMoney1 = new JLabel("Money: " + Global.currentPlayer.getMoney());
		lblMoney1.setBounds(48, 6, 82, 16);
		lblMoney1.setForeground(Color.white);
		add(lblMoney1);
  
		JLabel lblWater1 = new JLabel("Water");
		lblWater1.setBounds(32, 52, 35, 16);
		lblWater1.setForeground(Color.white);
		add(lblWater1);
  
		JLabel lblFurs1 = new JLabel("Furs");
		lblFurs1.setBounds(39, 132, 27, 16);
		lblFurs1.setForeground(Color.white);
		add(lblFurs1);
  
		JLabel lblFood1 = new JLabel("Food");
		lblFood1.setBounds(36, 114, 31, 16);
		lblFood1.setForeground(Color.white);
	 	add(lblFood1);
	  
		JLabel lblOre1 = new JLabel("Ore");
		lblOre1.setBounds(45, 93, 22, 16);
		lblOre1.setForeground(Color.white);
		add(lblOre1);
	  
		JLabel lblGames1 = new JLabel("Games");
	  	lblGames1.setBounds(25, 73, 42, 16);
	  	lblGames1.setForeground(Color.white);
	  	add(lblGames1);
	  
	  	JLabel lblFirearms1 = new JLabel("Firearms");
	  	lblFirearms1.setBounds(13, 150, 54, 16);
	  	lblFirearms1.setForeground(Color.white);
	  	add(lblFirearms1);
	  
	  	JLabel lblRobots1 = new JLabel("Robots");
	  	lblRobots1.setBounds(22, 232, 44, 16);
	  	lblRobots1.setForeground(Color.white);
	  	add(lblRobots1);
	  
	  	JLabel lblMachines1 = new JLabel("Machines");
	  	lblMachines1.setBounds(7, 211, 59, 16);
	  	lblMachines1.setForeground(Color.white);
	  	add(lblMachines1);
	  
	  	JLabel lblMedicine1 = new JLabel("Medicine");
	  	lblMedicine1.setBounds(7, 192, 56, 16);
	  	lblMedicine1.setForeground(Color.white);
	  	add(lblMedicine1);
  
	  	JLabel lblNarcotics1 = new JLabel("Narcotics");
	  	lblNarcotics1.setBounds(6, 172, 60, 16);
	  	lblNarcotics1.setForeground(Color.white);
	  	add(lblNarcotics1);
	  
	  	waterToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("water"));
	  	waterToSell1.setBounds(73, 51, 28, 20);
	  	waterToSell1.addActionListener(new ButtonListener1("water", 1));
	  	add(waterToSell1);
	  
	  	gamesToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("games"));
	  	gamesToSell1.setBounds(73, 70, 28, 21);
	  	gamesToSell1.addActionListener(new ButtonListener1("games", 1));
	  	add(gamesToSell1);
	  
	  	oreToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("ore"));
	  	oreToSell1.setBounds(73, 90, 28, 21);
	  	oreToSell1.addActionListener(new ButtonListener1("ore", 1));
	  	add(oreToSell1);
	  	
	  	foodToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("food"));
	  	foodToSell1.setBounds(73, 110, 28, 20);
	  	foodToSell1.addActionListener(new ButtonListener1("food", 1));
	  	add(foodToSell1);
	  
	  	furToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("furs"));
	  	furToSell1.setBounds(73, 129, 28, 21);
	  	furToSell1.addActionListener(new ButtonListener1("furs", 1));
	  	add(furToSell1);
  
	  	fireToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("firearms"));
	  	fireToSell1.setBounds(73, 149, 28, 21);
	  	fireToSell1.addActionListener(new ButtonListener1("firearms", 1));
	  	add(fireToSell1);
  
	  	narcToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("narcotics"));
	  	narcToSell1.setBounds(73, 169, 28, 21);
	  	narcToSell1.addActionListener(new ButtonListener1("narcotics", 1));
	  	add(narcToSell1);
    
	  	medsToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("medicine"));
	  	medsToSell1.setBounds(73, 189, 28, 20);
	  	medsToSell1.addActionListener(new ButtonListener1("medicine", 1));
	  	add(medsToSell1);
  
	  	machineToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("machines"));
	  	machineToSell1.setBounds(73, 208, 28, 21);
	  	machineToSell1.addActionListener(new ButtonListener1("machines", 1));
	  	add(machineToSell1);
  
	  	robotsToSell1 = new JButton(Global.currentPlayer.getShip().getGoodAmount("robots"));
	  	robotsToSell1.setBounds(73, 228, 28, 21);
	  	robotsToSell1.addActionListener(new ButtonListener1("robots", 1));
	  	add(robotsToSell1);

  
	  	waterGood1.updatePrice(level);
	  	furGood1.updatePrice(level);
	  	foodGood1.updatePrice(level);
	  	oreGood1.updatePrice(level);
	  	gamesGood1.updatePrice(level);
	  	fireGood1.updatePrice(level);
	  	medsGood1.updatePrice(level);
	  	machineGood1.updatePrice(level);
	  	narcGood1.updatePrice(level);
	  	robotGood1.updatePrice(level);
  
	  	JLabel lblSell = new JLabel("Sell");
	  	lblSell.setBounds(78, 34, 61, 16);
	  	lblSell.setForeground(Color.white);
	  	add(lblSell);
  
	  	waterToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/waterGood1.getCurrentPrice());
	  	waterToBuy1.setBounds(113, 50, 28, 20);
	  	waterToBuy1.addActionListener(new ButtonListener1("water", -1));
	  	add(waterToBuy1);
  
	  	gamesToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/gamesGood1.getCurrentPrice());
	  	gamesToBuy1.setBounds(113, 69, 28, 21);
	  	gamesToBuy1.addActionListener(new ButtonListener1("games", -1));
	  	add(gamesToBuy1);
  
	  	oreToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/oreGood1.getCurrentPrice());
	  	oreToBuy1.setBounds(113, 89, 28, 21);
	  	oreToBuy1.addActionListener(new ButtonListener1("ore", -1));
	  	add(oreToBuy1);
  
	  	foodToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/foodGood1.getCurrentPrice());
	  	foodToBuy1.setBounds(113, 109, 28, 20);
	  	foodToBuy1.addActionListener(new ButtonListener1("food", -1));
	  	add(foodToBuy1);
  
	  	furToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/furGood1.getCurrentPrice());
	  	furToBuy1.setBounds(113, 128, 28, 21);
	  	furToBuy1.addActionListener(new ButtonListener1("furs", -1));
	  	add(furToBuy1);
  
	  	fireToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/fireGood1.getCurrentPrice());
	  	fireToBuy1.setBounds(113, 148, 28, 21);
	  	fireToBuy1.addActionListener(new ButtonListener1("firearms", -1));
	  	add(fireToBuy1);
  
	  	narcToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/narcGood1.getCurrentPrice());
	  	narcToBuy1.setBounds(113, 168, 28, 21);
	  	narcToBuy1.addActionListener(new ButtonListener1("narcotics", -1));
	  	add(narcToBuy1);
  
	  	medsToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/medsGood1.getCurrentPrice());
	  	medsToBuy1.setBounds(113, 188, 28, 20);
	  	medsToBuy1.addActionListener(new ButtonListener1("medicine", -1));
	  	add(medsToBuy1);
  
	  	machinesToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/machineGood1.getCurrentPrice());
	  	machinesToBuy1.setBounds(113, 207, 28, 21);
	  	machinesToBuy1.addActionListener(new ButtonListener1("machines", -1));
	  	add(machinesToBuy1);
  
	  	robotToBuy1 = new JButton("" + Global.currentPlayer.getMoney()/robotGood1.getCurrentPrice());
	  	robotToBuy1.setBounds(113, 227, 28, 21);
	  	robotToBuy1.addActionListener(new ButtonListener1("robots", -1));
	  	add(robotToBuy1);
  
	  	waterP1 = new JLabel(waterGood1.getCurrentPrice() +" credits");
	  	waterP1.setBounds(150, 50, 150, 21);
	  	waterP1.setForeground(Color.white);
	  	add(waterP1);
    
	  	gamesP1 = new JLabel(gamesGood1.getCurrentPrice() +" credits");
	  	gamesP1.setBounds(150, 69, 150, 21);
	  	gamesP1.setForeground(Color.white);
	  	add(gamesP1);
    
	  	oreP1 = new JLabel(oreGood1.getCurrentPrice() +" credits");
	  	oreP1.setBounds(150, 89, 150, 21);
	  	oreP1.setForeground(Color.white);
	  	add(oreP1);
    
	  	foodP1 = new JLabel(foodGood1.getCurrentPrice() +" credits");
	  	foodP1.setBounds(150, 109, 150, 21);
	  	foodP1.setForeground(Color.white);
	  	add(foodP1);
  
	  	fursP1 = new JLabel(furGood1.getCurrentPrice() +" credits");
	  	fursP1.setBounds(150, 128, 150, 21);
	  	fursP1.setForeground(Color.white);
	  	add(fursP1);
  
	  	firearmsP1 = new JLabel(fireGood1.getCurrentPrice() +" credits");
	  	firearmsP1.setBounds(150, 148, 150, 21);
	  	firearmsP1.setForeground(Color.white);
	  	add(firearmsP1);
  
	  	narcoticsP1 = new JLabel(narcGood1.getCurrentPrice() +" credits");
	  	narcoticsP1.setBounds(150, 168, 150, 21);
	  	narcoticsP1.setForeground(Color.white);
	  	add(narcoticsP1);
  
	  	medicineP1 = new JLabel(medsGood1.getCurrentPrice() +" credits");
	  	medicineP1.setBounds(150, 188, 150, 21);
	  	medicineP1.setForeground(Color.white);
	  	add(medicineP1);
  
	  	machinesP1 = new JLabel(machineGood1.getCurrentPrice() +" credits");
	  	machinesP1.setBounds(150, 207, 150, 21);
	  	machinesP1.setForeground(Color.white);
	  	add(machinesP1);
  
	  	robotsP1 = new JLabel(robotGood1.getCurrentPrice() +" credits");
	  	robotsP1.setBounds(150, 227, 150, 21);
	  	robotsP1.setForeground(Color.white);
	  	add(robotsP1);
  
	  	JLabel lblBuy1 = new JLabel("Buy");
	  	lblBuy1.setBounds(115, 32, 61, 16);
	  	lblBuy1.setForeground(Color.white);
	  	add(lblBuy1);
	  	
	  	JButton btnDone1 = new JButton("Done!");
	  	btnDone1.setBounds(100, 260, 117, 29);
	  	btnDone1.addActionListener(new ActionListener(){
	  		

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Global.currentPlayer.setMoney(Global.currentPlayer.getMoney());
				getGamePanel().updateMoney();
				getGamePanel().updateFuel();
				getGamePanel().updatePlayer();
				getGamePanel().updateSellButtons();
				getGamePanel().updateBuyButtons();
				getTradePanel().frame.setVisible(false);
				
			}
	  		
	  	});
	  	add(btnDone1);
	  	
	  	BufferedImage myPicture;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("config_screen.png");
			myPicture = ImageIO.read(input);
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			picLabel.setBounds(0, 0, 450, 300);
			add(picLabel);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Background Not Loaded");
		}
	}
	
	public GamePanel getGamePanel(){
		return gp;
	}
	
	/**
	 * gets the game panel
	 * @return the game panel
	 */
	public TradePanel getTradePanel(){
		return TradePanel.this;		
	}
	
	/**
	 * this watches the window's closing buttons
	 * @author kateharlan
	 */
	@SuppressWarnings("unused")
	private class Closer implements WindowListener{

		@Override
		public void windowActivated(WindowEvent arg0) {}
		
		/**
		 * happens if the window closes
		 */
		@Override
		public void windowClosed(WindowEvent arg0) {
			Global.currentPlayer.setMoney(Global.currentPlayer.getMoney());
			getGamePanel().updateMoney();
			getGamePanel().updateFuel();
			getGamePanel().updatePlayer();
			getGamePanel().updateSellButtons();
			getGamePanel().updateBuyButtons();
		}

		/**
		 * happens as the window closes
		 */
		@Override
		public void windowClosing(WindowEvent arg0) {
			Global.currentPlayer.setMoney(Global.currentPlayer.getMoney());
			getGamePanel().updateMoney();
			getGamePanel().updateFuel();
			getGamePanel().updatePlayer();
			getGamePanel().updateSellButtons();
			getGamePanel().updateBuyButtons();
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {}

		@Override
		public void windowDeiconified(WindowEvent arg0) {}

		@Override
		public void windowIconified(WindowEvent arg0) {}

		@Override
		public void windowOpened(WindowEvent arg0) {}
		
	}
	
	
	/**
	 * button listener for buying and selling.
	 * @author kateharlan
	 *
	 */
	private class ButtonListener1 implements ActionListener{
		int action;
		String goodName;
  
		/**
		 * constructor.  
		 * @param goodName has to be formatted correctly
		 * @param action -1 is buying, 1 is selling
		 */
		public ButtonListener1(String goodName, int action){
			this.action=action;
			this.goodName=goodName;
		}
		
		/**
		 * makes buying and selling buttons work.
		 */
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if (action==-1){ //This is the same as buying
				Good good = Global.currentPlayer.getShip().goodConverter(goodName);//need to pass in the good name
				good.updatePrice(level); //We DID dynamically get the tech level
    
				if((Global.currentPlayer.getMoney()-good.getCurrentPrice()) > 0)
				{
					boolean x = Global.currentPlayer.getShip().addCargo(good);
					if (x == false){
						JOptionPane.showMessageDialog(null, "Your ship's cargo hold is full!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
					}
					else {
						Global.currentPlayer.setMoney(Global.currentPlayer.getMoney()- good.getCurrentPrice());
					}
					updateMoney1();
				}
				else {
					JOptionPane.showMessageDialog(null, "You don't have enough money to buy this item!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
				}
				updateSellButtons1();
				updateBuyButtons1();
			}
			else if (action==1){//We are selling 
				if(Global.currentPlayer.getShip().contains(goodName))
				{
					Global.currentPlayer.getShip().removeCargo(goodName);
					Good good = Global.currentPlayer.getShip().goodConverter(goodName);
					good.updatePrice(level); //We need to dynamically get the tech level
					Global.currentPlayer.setMoney(Global.currentPlayer.getMoney()+good.getCurrentPrice());
					updateMoney1();
				}
				else {
					JOptionPane.showMessageDialog(null, "You can't sell an item you don't have!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
				}
				updateBuyButtons1();
				updateSellButtons1();
			}
		}
	}
 
	/**
	 * loads the text for the sell buttons.
	 */
	public void updateSellButtons1()
	{
		waterToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("water"));
		furToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("furs"));
		foodToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("food"));
		oreToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("ore"));
		gamesToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("games"));
		fireToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("firearms"));
		robotsToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("robots"));
		machineToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("machines"));
		medsToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("medicine"));
		narcToSell1.setText(Global.currentPlayer.getShip().getGoodAmount("narcotics"));
	}
 
	/**
	 * loads the text for the buy buttons.
	 */
	public void updateBuyButtons1()
	{
		waterToBuy1.setText("" + Global.currentPlayer.getMoney()/waterGood1.getCurrentPrice());
		gamesToBuy1.setText("" + Global.currentPlayer.getMoney()/gamesGood1.getCurrentPrice());
		oreToBuy1.setText("" + Global.currentPlayer.getMoney()/oreGood1.getCurrentPrice());
		foodToBuy1.setText("" + Global.currentPlayer.getMoney()/foodGood1.getCurrentPrice());
		furToBuy1.setText("" + Global.currentPlayer.getMoney()/furGood1.getCurrentPrice());
		fireToBuy1.setText("" + Global.currentPlayer.getMoney()/fireGood1.getCurrentPrice());
		narcToBuy1.setText("" + Global.currentPlayer.getMoney()/narcGood1.getCurrentPrice());
		medsToBuy1.setText("" + Global.currentPlayer.getMoney()/medsGood1.getCurrentPrice());
		machinesToBuy1.setText("" + Global.currentPlayer.getMoney()/machineGood1.getCurrentPrice());
		robotToBuy1.setText("" + Global.currentPlayer.getMoney()/robotGood1.getCurrentPrice());
	}
 
	/**
	 * sets up the player
	 * @param p a player
	 */
	public void updatePlayer1(Player p)
	{
		Player pp = Global.currentPlayer;
		lblName1.setText("Name: " + pp.getName()); 
	}
 
	/**
	 * updates the money label
	 */
	public void updateMoney1()
	{
		lblMoney1.setText("Money: " + Global.currentPlayer.getMoney());	}
 
	/**
	 * sets up all the goods
	 */
	public void activateGoods() {
		waterGood1 = new Good ("Water", 0, 0, 30, 3, 4, "LOTSOFWATER", "DESERT", 30, 50, 0);
		furGood1 = new Good ("Furs", 0, 0, 250, 10, 10, "RICHFAUA", "LIFELESS", 230, 280, 0);
		foodGood1 = new Good ("Food", 1, 0, 100, 5, 5, "RICHSOIL", "POORSOIL", 90, 160, 0);
		oreGood1 = new Good ("Ore", 2, 2, 350, 20, 10, "MINERALRICH", "MINERALPOOR", 350, 420, 0);
		gamesGood1 = new Good ("Games", 3, 1, 250, -10, 5, "ARTISTIC", null, 160, 270, 0);
		fireGood1 = new Good ("Firearms", 3, 1, 1250, -75, 100, "WARLIKE", null, 600, 1100, 0);
		medsGood1 = new Good ("Medicine", 4, 1, 650, -20, 10, "LOTSOFHERBS", null, 400, 700, 0);
		machineGood1 = new Good ("Machines", 4, 3, 900, -30, 5, null, null, 600, 800, 0);
		narcGood1 = new Good ("Narcotics", 5, 0, 3500, -125, 150, "WEIRDMUSHROOMS", null, 2000, 3000, 0);
		robotGood1 = new Good ("Robots", 6, 4, 5000, -150, 100, null, null, 3500, 5000, 0);
	}
}